package br.com.main.client.system.morador;

import java.util.Date;

import br.com.main.client.ComunServico;
import br.com.main.client.ComunServicoAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.TextBox;

public class MeusDadosPresenterM {
	Display display;
	DeckPanel deck;
	
	public interface Display {

		TextBox getTbNome();

		TextBox getTbCpf();

		TextBox getTbLogin();

		TextBox getTbEmail();

		TextBox getTbTelefone();

		TextBox getTbData_nasc();

		Button getBtEditar();

		Button getBtSalvar();

		Button getBtCancel();
		
	}

	public MeusDadosPresenterM(Display display, DeckPanel deck) {
		super();
		this.display = display;
		this.deck = deck;
		bind();
		loadFields();
	}
	
	public void bind() {
		
		display.getBtEditar().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent arg0) {
				
				changleEnable(true);
				
				display.getBtSalvar().setVisible(true);
				display.getBtCancel().setVisible(true);
				display.getBtEditar().setVisible(false);
			}
		});
		
		display.getBtSalvar().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent arg0) {
				
				changleEnable(false);
				
				display.getBtSalvar().setVisible(false);
				display.getBtCancel().setVisible(false);
				display.getBtEditar().setVisible(true);
				
				String[] novosDados = new String[5];
				novosDados[0] = display.getTbNome().getText();
				novosDados[1] = display.getTbLogin().getText();
				novosDados[2] = display.getTbEmail().getText();
				novosDados[3] = display.getTbTelefone().getText();
				novosDados[4] = display.getTbData_nasc().getText();
				
				
				getServico().updateMorador(novosDados, new AsyncCallback<Void>() {
					
					@Override
					public void onSuccess(Void arg0) {
						Window.alert("Alterado com sucesso!");
					}
					
					@Override
					public void onFailure(Throwable arg0) {
						// TODO Auto-generated method stub
						
					}
				});
				
			}
		});
		
		display.getBtCancel().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent arg0) {

				loadFields();
				
				display.getBtSalvar().setVisible(false);
				display.getBtCancel().setVisible(false);
				display.getBtEditar().setVisible(true);
			}
		});
	}
	
	public void loadFields () {
		getServico().carregaDadosMorador(new AsyncCallback<String[]>() {
			
			@SuppressWarnings("deprecation")
			@Override
			public void onSuccess(String[] result) {

				DateTimeFormat format = DateTimeFormat.getFormat("d/MM/y");

				String dataNasc = format.format(new Date(result[5]));
				
				display.getTbNome().setText(result[1]);
				display.getTbCpf().setText(result[0]);
				display.getTbLogin().setText(result[2]);
				display.getTbEmail().setText(result[3]);
				display.getTbTelefone().setText(result[4]);
				display.getTbData_nasc().setText(dataNasc);
				
				changleEnable(false);
			}
			
			@Override
			public void onFailure(Throwable arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	public void changleEnable (boolean status) {
		
		display.getTbNome().setEnabled(status);
		display.getTbLogin().setEnabled(status);
		display.getTbEmail().setEnabled(status);
		display.getTbTelefone().setEnabled(status);
		display.getTbData_nasc().setEnabled(status);
		
	}
	
	private ComunServicoAsync getServico() {
		return GWT.create(ComunServico.class);
	}
}